package fun.zhufn.oldcomm.model.service;

import fun.zhufn.oldcomm.model.bo.User;
import fun.zhufn.oldcomm.model.bo.customlist.UserList;
import fun.zhufn.oldcomm.model.bo.json.JsonList;
import fun.zhufn.oldcomm.model.dao.GlobalDataDao;

import java.util.List;
import java.util.logging.Logger;

public class UserManagerService {
    private static final UserManagerService INSTANCE = new UserManagerService();
    public static UserManagerService getInstance() {
        return INSTANCE;
    }
    private UserManagerService() {}
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    private User user = null;
    private static final Logger log = Logger.getLogger(UserManagerService.class.toString());
    private static final GlobalDataDao dataDao = GlobalDataDao.getInstance();

    /**
     * @return 用户列表
     */
    public UserList getSavedUserListOrDefault() {
        UserList users_l = getUserList();
        if (users_l.getData().isEmpty()) {
            log.info("无用户，生成默认管理员用户root密码123");
            addUser(new User(
                    "root", "123", "root", "00000000", "00000000000", true, 3
            ));
            users_l = getUserList();
        }
        return users_l;
    }

    /**
     * @param username 用户名
     * @param pwd 密码
     * @return 消息
     */
    public String doLogin(String username, String pwd) {
        UserList users_l = getSavedUserListOrDefault();
        List<User> ul = users_l.getData();
        for (User v: ul) {
            if (v.getAccount().equals(username)) {
                if (v.getPwd().equals(pwd)) {
                    log.info("用户登录成功");
                    setUser(v);
                    return "OK";
                } else {
                    return "用户密码错误";
                }
            }
        }
        return "用户不存在";
    }

    /**
     * @return 用户列表
     */
    public UserList getUserList() {
        UserList users_l = new UserList();
        JsonList users_jl = dataDao.getUserJsonList();
        users_l.fromJsonList(users_jl);
        return users_l;
    }

    /**
     * @param u 用户
     * @return 是否成功
     */
    public boolean addUser(User u) {
        UserList ul = getUserList();
        List<User> uld = ul.getData();
        for (var v: uld) {
            if (v.getAccount().equals(u.getAccount())) {
                return false;
            }
        }
        ul.add(u);
        dataDao.updateUserJsonList(ul.toJsonList());
        try {
            dataDao.saveToFile();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @param acc 用户账号
     * @return 是否成功
     */
    public boolean removeUser(String acc) {
        UserList ul = getUserList();
        List<User> uld = ul.getData();
        for (int i = 0; i < uld.size(); ++i) {
            if (uld.get(i).getAccount().equals(acc)) {
                uld.remove(i);
                ul.setData(uld);
                dataDao.updateUserJsonList(ul.toJsonList());
                try {
                    dataDao.saveToFile();
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
        }
        return false;
    }
}
